package com.thinksns.sociax.t4.android.popupwindow;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.adapter.EventPopListAdapter;

import java.util.List;

/**
 * Created by zhiyicx on 2017/5/2.
 */

public class EventPopWindow {
    private List<String> datas;
    private Activity activity;
    private ListView listView;
    private EventPopListAdapter adapter;
    private PopupWindow popupWindow;
    private int animationStyle;
    private DismissListener dismissDialogListener;
    private int mPosition;

    public EventPopWindow(Activity activity,List<String> datas,int animationStyle, int width, int height){
        this.activity = activity;
        this.datas = datas;
        this.animationStyle = animationStyle;
        initPop(width,height);
    }

    private void initPop(int width,int heigh){
        View view = LayoutInflater.from(activity).inflate(R.layout.event_pop_layout,null);
        listView = (ListView) view.findViewById(R.id.event_pop_list);
        adapter = new EventPopListAdapter(activity);
        adapter.setDatas(datas);
        listView.setAdapter(adapter);
        popupWindow = new PopupWindow(view,width,heigh);
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        // 设置popwindow如果点击外面区域，便关闭。
        popupWindow.setOutsideTouchable(true);
        if (animationStyle != 0) {
            popupWindow.setAnimationStyle(animationStyle);
        }

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setWindowNormal();
                if (dismissDialogListener != null) {
                    dismissDialogListener.onDismiss(mPosition);
                }
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mPosition = i;
                popupWindow.dismiss();
            }
        });
    }

    public interface DismissListener{
        void onDismiss(int i);
    }

    public void setDismissDialogListener(DismissListener dismissDialogListener){
        this.dismissDialogListener = dismissDialogListener;
    }

    public void setAnimationStyle(int animationStyle){
        this.animationStyle = animationStyle;
    }

    /**
     * 设置窗口正常:有些界面不能够这样使用窗口直接透明化的方法，来设置popwindow的方法，但毕竟是少数，在子类中重写该方法返回null
     */
    protected void setWindowNormal() {
        setWindowAlpha(1.0f);
    }

    /**
     * 设置屏幕的透明度
     *
     * @param alpha 需要设置透明度
     */
    private void setWindowAlpha(float alpha) {
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();
        params.alpha = alpha;
        params.verticalMargin = 100;
        activity.getWindow().setAttributes(params);
    }

    /**
     * 显示popWindow :整个界面中的位置
     */

    public void showPopAtLocation(View parent, int offX, int offY, int gravity) {
        setWindowAlpha();
        // 设置popwindow显示位置
        popupWindow.showAtLocation(parent, gravity, offX, offY);
        if (Build.VERSION.SDK_INT != 24)
            popupWindow.update();
        //startAnim();
    }

    /**
     * 显示popWindow :相对于控件
     */
    @TargetApi(19)
    public void showPopAsDropDown(View parent, int offX, int offY, int gravity) {
        setWindowAlpha();
        // 设置popwindow显示位置
        try {
            popupWindow.showAsDropDown(parent, offX, offY, gravity);
        }catch (Throwable t){
            popupWindow.showAsDropDown(parent, offX, offY);
        }
        if (Build.VERSION.SDK_INT != 24)
            popupWindow.update();
        //startAnim();
    }


    /**
     * 显示popWindow,在屏幕的最下方
     */
    public void showPopAtDown(View parent) {
        setWindowAlpha();
        // 设置popwindow显示位置
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        if (Build.VERSION.SDK_INT != 24)
            popupWindow.update();
    }

    protected void setWindowAlpha() {
        setWindowAlpha(0.8f);
    }
}
